package com.seago.iceberg.config;

import org.apache.iceberg.hive.HiveCatalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Title:
 * <p>
 * description:
 *
 * @Company: 中国船舶集团海舟系统技术有限公司-武汉
 * @author: yangs
 * @since: 2023-05-06 15:34
 * @version: 1.0
 **/
@Configuration
public class IcebergConfig {

	@Autowired
	IcebergConfiguration icebergConfiguration;

	@Bean
	public HiveCatalog hiveCatalog() {
		Map<String, String> properties = new HashMap<>();
		properties.put("type", "iceberg");
		properties.put("clients", "5");
		properties.put("property-version", "1");
		properties.put("warehouse", icebergConfiguration.getCatalog().getWarehouse());
		properties.put("catalog-type", "hive");
		properties.put("uri", icebergConfiguration.getCatalog().getUri());
		// 创建 Hadoop Configuration
		org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
		conf.set("fs.s3a.connection.ssl.enabled", "false");
		conf.set("fs.s3a.endpoint", icebergConfiguration.getHadoop().getFsS3aEndpoint());
		conf.set("fs.s3a.access.key", icebergConfiguration.getHadoop().getFsS3aAccess());
		conf.set("fs.s3a.secret.key", icebergConfiguration.getHadoop().getFsS3aSecret());
		conf.set("fs.s3a.path.style.access", "true");
		conf.set("fs.s3a.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem");
		conf.set("fs.s3a.fast.upload", "true");

		HiveCatalog catalog = new HiveCatalog();
		catalog.setConf(conf);

		// 初始化catalog
		catalog.initialize("hive_catalog", properties);
		return catalog;
	}

}
